//
//  TTCache.h
//  Tint
//
//  Created by Kevin Wu on 8/23/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Core/Core.h"
#import "Additions/Additions.h"

// Dictionary
//   type1 -> Dictionary
//              key1 -> item1
//              key2 -> item2
//              key3 -> item3
//   type2 -> Dictionary
//              key1 -> item1
//              key2 -> item2
//              key3 -> item3
//   type3 -> Dictionary
//              key1 -> item1
//              key2 -> item2
//              key3 -> item3


#define TTCacheItemKey @"TTCacheItemKey"
#define TTCacheItemAddDate @"TTCacheItemAddDate"
#define TTCacheItemUpdateDate @"TTCacheItemUpdateDate"
#define TTCacheItemExpirationDate @"TTCacheItemExpirationDate"
// Disk
#define TTCacheItemFilePath @"TTCacheItemFilePath"
#define TTCacheItemFileSize @"TTCacheItemFileSize"
// Memory
#define TTCacheItemCacheObject @"TTCacheItemCacheObject"



#define TTCacheNameDefault @"default"



@interface TTCache : NSObject {
    NSTimeInterval _defaultTimeoutInterval;
    
    NSMutableDictionary *_cacheDictionary;
    
    NSLock *_executeLock;
}

@property(nonatomic, assign) NSTimeInterval defaultTimeoutInterval; // Default: 7 days

+ (id)sharedObject;
+ (BOOL)hasSharedObject;
+ (void)destroySharedObject;


- (BOOL)hasCacheForKey:(NSString *)key;
- (BOOL)hasCacheForKey:(NSString *)key name:(NSString *)name;

- (void)removeCacheForKey:(NSString *)key;
- (void)removeCacheForKey:(NSString *)key name:(NSString *)name;


- (void)cleanAllCache;
- (void)cleanCache;
- (void)cleanCacheByName:(NSString *)name;

- (void)clearAllCache;
- (void)clearCache;
- (void)clearCacheByName:(NSString *)name;


- (NSUInteger)totalCount;
- (NSUInteger)count;
- (NSUInteger)countOfName:(NSString *)name;

- (NSArray *)cacheNames;
- (NSArray *)cacheItemKeys;
- (NSArray *)cacheItemKeysOfName:(NSString *)name;


- (NSDate *)addDateOfKey:(NSString *)key;
- (NSDate *)addDateOfKey:(NSString *)key name:(NSString *)name;

- (NSDate *)updateDateOfKey:(NSString *)key;
- (NSDate *)updateDateOfKey:(NSString *)key name:(NSString *)name;

- (NSDate *)expirationDateOfKey:(NSString *)key;
- (NSDate *)expirationDateOfKey:(NSString *)key name:(NSString *)name;

- (NSString *)filePathOfKey:(NSString *)key;
- (NSString *)filePathOfKey:(NSString *)key name:(NSString *)name;

- (int)fileSizeOfKey:(NSString *)key;
- (int)fileSizeOfKey:(NSString *)key name:(NSString *)name;

- (id)cacheObjectOfKey:(NSString *)key;
- (id)cacheObjectOfKey:(NSString *)key name:(NSString *)name;


////////////////////////////////////////////////////////////////////////////////

- (NSMutableDictionary *)addItemWithKey:(NSString *)key name:(NSString *)name timeoutInterval:(NSTimeInterval)timeoutInterval;
- (BOOL)createCacheForName:(NSString *)name;
- (BOOL)isItemValid:(NSDictionary *)item;

@end
